package hlhj.fhp.burst.activitys

import android.Manifest
import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.*
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.util.Log
import android.widget.Toast
import com.tbruyelle.rxpermissions.RxPermissions
/*import com.cjt2325.cameralibrary.JCameraView
import com.cjt2325.cameralibrary.listener.JCameraListener*/
import com.tenma.ventures.base.TMActivity
import hlhj.fhp.burst.R
import hlhj.fhp.burst.bean.TempBean
import hlhj.fhp.burst.camera.JCameraView
import hlhj.fhp.burst.camera.listener.ErrorListener
import hlhj.fhp.burst.camera.listener.JCameraListener
import hlhj.fhp.burst.camera.util.LogUtil
import kotlinx.android.synthetic.main.activity_camera_aty.*
import org.greenrobot.eventbus.EventBus
import java.io.File

class CameraAty : TMActivity() {
    private val CAMERA_VIDEO=110
    private val CAMERA_IMG=120
    private var type=0;
    private lateinit var bitmap:Bitmap
    private lateinit var path:String
    private val handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            LogUtil.e("info","跳转到handler${type}")
            this@CameraAty.finish()
            when(type){
                CAMERA_VIDEO->{
//                    finish()
                    intent.putExtra("type",CAMERA_VIDEO)
                    intent.putExtra("bitmap",bitmap)
                    intent.putExtra("path",path)
                    setResult(-1,intent)
                }
                CAMERA_IMG->{
                    intent.putExtra("type",CAMERA_IMG)
                    intent.putExtra("bitmap",bitmap)
                    setResult(-1,intent)
                }
            }
            finish()
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera_aty)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this@CameraAty, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this,"没有权限",Toast.LENGTH_SHORT).show()

                ActivityCompat.requestPermissions(this@CameraAty,
                        arrayOf<String>(Manifest.permission.RECORD_AUDIO),1
                );
            }else {

            }
        }
        //设置视频保存路径
        camera.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");

//设置视频质量
        camera.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE)
        camera.setErrorLisenter(object :ErrorListener{
            override fun onError() {
                LogUtil.e("info","录制失败")
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun AudioPermissionError() {

                LogUtil.e("info","录音权限")

//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }
        })
        camera.setJCameraLisenter(object : JCameraListener {
            override fun recordSuccess(url: String?, firstFrame: Bitmap?) {
                LogUtil.e("info","录制完成")

                type=CAMERA_VIDEO
                bitmap=firstFrame!!
                path=url!!

                val bean = TempBean(firstFrame, url, CAMERA_VIDEO)
                EventBus.getDefault().post(bean)
                finish()
//                handler.sendEmptyMessage(1)
                LogUtil.e("info","录制完成6")

            }

            override fun captureSuccess(bit: Bitmap?) {
                val bean = TempBean(bit, "", CAMERA_IMG)
                EventBus.getDefault().post(bean)
                finish()
     /*           type=CAMERA_IMG
                bitmap=bit!!*/
             /*   intent.putExtra("type",CAMERA_IMG)
                intent.putExtra("bitmap",bitmap)
                setResult(-1,intent)
                finish()*/

            }
        })
        camera.setLeftClickListener {
            finish()
        }
        camera.setRightClickListener {
            LogUtil.e("info","又不点击")
            when(type){
                CAMERA_VIDEO->{
                    intent.putExtra("type",CAMERA_VIDEO)
                    intent.putExtra("bitmap",bitmap)
                    intent.putExtra("path",path)
                    setResult(-1,intent)
                }
                CAMERA_IMG->{
                    intent.putExtra("type",CAMERA_IMG)
                    intent.putExtra("bitmap",bitmap)
                    setResult(-1,intent)
                }
            }
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        camera.onResume()
    }

    override fun onPause() {
        super.onPause()
        camera.onPause()
    }
}
